package vip.xiaonuo.issued.impl.visitor;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.iservice.IVisitorDeviceService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.tanscation.TranCationCallBackService;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.mapper.VisitorMapper;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.facedoor.remote.iservice.adapter.VisitorReqAdapter;

import javax.annotation.Resource;
import java.util.Objects;
import vip.xiaonuo.facedoor.rule.RuleConstant;

@Service(value = RuleConstant.DW + RuleConstant.VISITOR_DEVICE)
@Slf4j
public class MqttVisitorDeviceServiceImpl implements IVisitorDeviceService {

  @Resource
  private VisitorMapper visitorMapper;

  @Resource
  private VisitorDeviceMapper visitorDeviceMapper;

  @Resource
  private VisitorReqAdapter personService;

  @Resource
  private TranCationCallBackService callBackService;


  private void baseCheck(VisitorDevice visitorDevice) {
    if (visitorDevice == null) {
      throw new ServiceException(500, "找不到关联信息！");
    }
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD) || visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE) || visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
      throw new ServiceException(500, "非正常状态 不允许操作！");
    }
  }


  @Override
  public void delete(Long id) throws Exception {
    VisitorDevice visitorDevice = visitorDeviceMapper.selectById(id);
    baseCheck(visitorDevice);
    // 新增失败 直接删除人员
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
      visitorDeviceMapper.deleteById(id);
    } else {
      visitorDevice.setStatus(MemberDeviceConstant.DELETE);
      visitorDeviceMapper.updateById(visitorDevice);
      personService.deletePerson(visitorDevice, visitorDevice.getDeviceId()+"");

    }
  }

  public static void main(String[] args) {
    System.out.println(System.getProperty("user.dir"));
  }

  @Override
  public void sync(Long id) throws Exception {
    VisitorDevice visitorDevice = this.visitorDeviceMapper.selectById(id);
    baseCheck(visitorDevice);
    if (visitorDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
      throw new ServiceException(500, "未发生错误 不允许同步！");
    }

    if (Objects.equals(visitorDevice.getStatus(), MemberDeviceConstant.ADD_FAIL)) {
      visitorDevice.setStatus(MemberDeviceConstant.ADD);
    } else if (Objects.equals(visitorDevice.getStatus(), MemberDeviceConstant.UPDATE_FAIL)) {
      visitorDevice.setStatus(MemberDeviceConstant.UPDATE);
    } else {
      visitorDevice.setStatus(MemberDeviceConstant.DELETE);
    }
    visitorDeviceMapper.updateById(visitorDevice);

    callBackService.execute(() -> {
      try {
        Visitor visitor = visitorMapper.selectById(visitorDevice.getVisitorId());
        // 恢复人员状态
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
          visitor.setMemberOrVisitorDeviceId(visitorDevice.getId());
          visitor.setMemberType(2);
          personService.addPerson(visitorDevice.getDeviceId() + "", visitor);
        }
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
          visitor.setMemberOrVisitorDeviceId(visitorDevice.getId());
          visitor.setMemberType(2);
          personService.updatePerson(visitorDevice.getDeviceId() + "", visitor);
        }
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
          personService.deletePerson(visitorDevice, visitorDevice.getDeviceId()+"");
        }
      } catch (Exception e) {
        log.error("重新同步人员发生错误【{}】", ExceptionUtils.getMessage(e));
      }
    });
  }
}
